package com.yuecai.utils;

import com.yuecai.exception.BadRequestException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class VerifyCode {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 校验验证码
     * @param captchaKey 后缀 例： mobile + "_" + type;
     * @param captcha 验证码
     * @return
     */
    public boolean checkoutCaptcha(String captchaKey, String captcha) {
        String code = stringRedisTemplate.opsForValue().get(captchaKey);
        if(captcha.equals(code)){
            stringRedisTemplate.delete(captchaKey);
            return true;
        }
        throw new BadRequestException("验证码不正确！");
    }

}
